package com.bml.architect.alibaba;

import com.bml.architect.alibaba.intercepter.MyLoadBalanced;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerInterceptor;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

import java.util.Collections;

@SpringBootApplication
@EnableFeignClients
public class ChannelApplication {

    @Autowired
    private LoadBalancerClient loadBalancer;

    public static void main(String[] args) throws InterruptedException {


        ConfigurableApplicationContext context = SpringApplication.run(ChannelApplication.class, args);

        for(;;){
            String name = context.getEnvironment().getProperty("common.name");
            String age = context.getEnvironment().getProperty("common.age");
            System.out.println("common.name="+name+";common.age="+age);
            Thread.sleep(3000);
        }
    }

    @Bean
    @LoadBalanced
    //@MyLoadBalanced //自定义的负载均衡
    public RestTemplate restTemplate(){
        //LoadBalancerInterceptor
        return new RestTemplate();
//        RestTemplate restTemplate = new RestTemplate();
//        restTemplate.setInterceptors(Collections.singletonList(new LoadBalancerInterceptor(loadBalancer)));
//        return restTemplate;
    }
}
